<template>
  <div style="width: 100%">
    <a-card class="card" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="vertical">
          <a-row :gutter="48">
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="准考证号">
                <a-input placeholder="请输入..." v-model="queryParam.ksbh" />
              </a-form-item>
            </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="姓名">
                <a-input placeholder="请输入..." v-model="queryParam.ksxm" />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="招生年度">
                  <a-select :allowClear="true" v-model="queryParam.zsnj" showSearch optionFilterProp="children">
                    <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="院系">
                  <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" v-model="queryParam.bkyxsm" @change="yxChange" showSearch
                    optionFilterProp="children">
                    <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+"-"+ i.dwmc }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="专业">
                  <a-select :allowClear="true" v-model="queryParam.bkzydm" showSearch optionFilterProp="children" @change="zyChange">
                    <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="研究方向">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.bkyjfxm">
            <a-select-option v-for="i in selectMenu.fx" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
            </a-select>
          </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="学习形式">
                  <a-select :allowClear="true" v-model="queryParam.zhxlxxxs" showSearch optionFilterProp="children">
                    <a-select-option v-for="i in selectMenu.xxlx" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="录取状态">
                  <a-select :allowClear="true" v-model="queryParam.lqzt" showSearch optionFilterProp="children">
                    <a-select-option v-for="i in TYPE0421" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :sm="{span:16}" :xl="{span:24}">
              <a-button type="primary" @click="loadData">查询</a-button>
              <a-button type="primary" @click="handleExport">导出</a-button>
              <a-button type="primary" @click="xsHandleExport">导出学生选定导师</a-button>
              <!-- <a-button type="primary" @click="handleSubmit" :disabled='!isDisable'>提交</a-button> -->
              <!-- <a-button @click="queryParam = {};loadData()" >重置</a-button> -->

              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <!-- <a-alert class="card"  message="温馨提示：请维护完本院系所有信息后再提交、一旦提交禁止再进行任何修改" banner /> -->
    <a-card class="card" :bordered="false">
      <a-alert :message="'总条数：' + pagination.total" type="info" show-icon />
      <a-table ref="table" size="default" rowKey="key" :columns="columns" :loading="loading" :dataSource="data"
        :pagination="pagination" @change="handleTableChange" :scroll="{ x: 2000 }">
        <template slot="ksbh" slot-scope="text,record">
          <a @click="handlePreview(record.id)">{{text}}</a>
        </template>
        <template slot="action" slot-scope="text,record">
          <a @click="(record.lqzt == '01')&& (record.shzt!='02') && handleReview(record.id)" :style="record.lqzt == '01' && record.shzt!='02'? '' : 'color:#999'">录取</a>
          <!-- <a @click="handleReview(record.id)" >录入(修改)</a> -->
          <!-- <a @click="(record.lqzt == '01') && (record.shzt!='02') && handleEnroll(record)" :style="record.lqzt == '01' && record.shzt!='02'? '' : 'color:#999'">录取</a> -->
          <a @click="(record.lqzt != '01') && (record.shzt!='02') && handleCancelEnroll(record)" :style="record.lqzt != '01' && record.shzt!='02'? '' : 'color:#999'">取消录取</a>
        </template>
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
      </a-table>
      <!-- 审查 -->
      <enroll-review @close="handleReviewClose" @success="handleReviewSuccess" :planReviewVisible="planReviewVisible"
        :info="planReviewData"></enroll-review>
      <!-- 查看 -->
      <examinee :previewVisible="previewVisible" :info="examineeData" @close="handleClosePreview"></examinee>
    </a-card>
  </div>
</template>

<script>
  import EnrollReview from './enrollReview'
  import Examinee from '../../examinee/detail'
  // import moment from 'moment'
  import {
    STable
  } from '~'
  import {
    mapState
  } from 'vuex'

  const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072', 'TYPE0421', 'TYPE0301']

  export default {
    components: {
      STable,
      EnrollReview,
      Examinee
    },
    data() {
      return {
        advanced: true,
        previewVisible: false,
        planReviewVisible: false,
        loading: false,
        data: [],
        // 查询参数
        queryParam: {
          zsnj: '',
          ksbh: '',
          ksxm: '',
          bkyxsm: '',
          bkzydm: '',
          zhxlxxxs: '',
          lqzt: ''
        },
        postParams: {},
        // 表头
        columns: [{
            title: '序号',
            scopedSlots: {
              customRender: 'serial'
            },
            width:80
          },
          {
            title: '准考证号',
            dataIndex: 'ksbh',
            scopedSlots: {
              customRender: 'ksbh'
            },
            width:150
          },
          {
            title: '姓名',
            dataIndex: 'ksxm',
            width:80
          },
          {
            title: '报考院系',
            dataIndex: 'bkyxsmc',
            width:100
          },
          {
            title: '报考专业代码',
            dataIndex: 'bkzydm',
            width:150
          },
          {
            title: '报考专业',
            dataIndex: 'bkzymc',
            width:100
          },

          {
            title: '研究方向',
            dataIndex: 'bkyjfxmc',
            width:100
          },
          {
            title: '初试成绩',
            dataIndex: 'zf',
            width:100
          },
          {
            title: '复试成绩',
            dataIndex: 'fscj',
            width:100
          },
          {
            title: '综合评定',
            dataIndex: 'zhpd',
            width:100
          },
          {
            title: '综合排名',
            dataIndex: 'zhpm',
            width:100
          },
          {
            title: '档案是否转入',
            dataIndex: 'dasfzr',
            width:150
          },
          {
            title: '培养模式',
            dataIndex: 'pymsm',
            width:100
          },
          {
            title: '录取类别',
            dataIndex: 'lqlbm',
            width:100
          },
          {
            title: '学制',
            dataIndex: 'xz',
            width:100
          },
          {
            title: '录取导师',
            dataIndex: 'bkdsyxm',
            width:100
          },
          {
            title: '录取状态',
            dataIndex: 'lqztmc',
            width:100
          },
          {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
        ],
        selectMenu: {
          yx: [], // 院系
          zy: [],
          xxlx: [],
        },
        planReviewData: {},
        examineeData: {},
        pagination: {
          current: 1,
          showSizeChanger: true,
          pageSizeOptions: ['10', '50', '100'],
          // 改变pageSize后触发
          onShowSizeChange: (current, pageSize) => this.pagination.pageSize = pageSize,
          showTotal: (() => {return `共 ${this.pagination.total} 条`})
        },
        isDisable: false
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'], // 年级
        TYPE0421: state => state.app['TYPE0421']
      })
    },
    created() {
      this.initSelectMenu()
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })

    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
          // 获取学习形式
          this.$api.base.dictionaryData('TYPE0441').then(res => {
            this.selectMenu.xxlx = res.data
          })
          this.queryParam.bkyxsm = res[0].data[0].dwh
          this.isSubmit()
          this.getProssion(this.queryParam.bkyxsm)
          // 获取当前学年
          this.$api.base.getSchoolYear().then(res => {
            this.queryParam.zsnj = res.data.masterAdmissionYear
            this.loadData({
              pageSize: 10,
              pageNum: 1
            })
          })
        })
      },
      // 选择院系
      yxChange(code) {
        this.$set(this.queryParam, 'bkzydm', '')
        this.$set(this.selectMenu, 'zy', [])
        if(code){

        this.getProssion(code)
        }
      },
      getProssion(code) {
        this.$api.base.majorAll({
          lsdwh: code
        }).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      },
      //选择专业
    zyChange(code){
       this.$set(this.queryParam, 'bkyjfxm', '')
        this.$set(this.selectMenu, 'fx', [])
        if(code){
          this.getDurection(code)
        }
    },
    getDurection(code) {
        this.$api.base.searchDirectory({
          ssyxm: this.queryParam.bkyxsm,
          zsxn: this.queryParam.zsnj,
          sszym: code,
           xwccm: 2
        }).then(res => {
          this.$set(this.selectMenu, 'fx', res.data)
        })
      },
      // 加载数据
      loadData(parameter = {}) {
        if (!parameter.pageNum) {
          this.pagination.current = 1
          parameter.pageNum = 1
        }
        let params = Object.assign(this.queryParam, parameter)
        this.loading = true
        this.$api.recruitstat.masterAdmissionManageList(params).then(res => {
          const pagination = { ...this.pagination
          };
          pagination.total = 0
          if (res.data && res.data.total) {
            pagination.total = res.data.total;
          }
          this.data = []
          this.pagination = pagination;
          if (res.data && res.data.rows.length) {
            this.data = res.data.rows;
			res.data.rows.map((item,index) => {
				this.TYPE0421.map((_item,_index) => {
					if(item.lqzt == _item.code){
						item.lqztmc = _item.name
					}
				})
				return item
			})
          }
          this.loading = false;
        })
      },
      // 分页
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = { ...this.pagination
        };
        pager.current = pagination.current;
        this.pagination = pager;
        this.loadData({
          pageSize: pagination.pageSize,
          pageNum: pagination.current
        });
      },
      // 多选
      onSelectChange(selectedRowKeys, selectedRows) {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        this.selectedRows = selectedRows
        this.selectedRowKeys = selectedRowKeys
      },
      isSubmit() {
        // 提交按钮是否存在
        this.$api.recruitstat.masterAdmissionManageIsSubmit({
          bkyxsm: this.queryParam.bkyxsm,
          zsnj: this.queryParam.zsnj
        }).then(res => {
          this.isDisable = res.data
        })
      },
      // 导出
      handleExport() {
        console.log('导出')
        this.$export('admissions/ssrqgl/export',this.queryParam)
      },
      xsHandleExport() {
        console.log('导出')
        this.$export('admissions/ssrqgl/exportXsxdds',this.queryParam)
      },
      // 审查
      handleReview(code) {
        console.log('审查')
        this.$api.recruitstat.masterAdmissionManageId(code).then(res => {
          this.planReviewData = res.data
        })
        // this.planReviewData = {
        //   zkzh: 'A100',
        //   ksxm: '小仙',
        //   lqds: '100',
        //   lqlb: '1',
        //   pyms: '4124',
        //   qezz: true
        // }
        this.planReviewVisible = true
      },
      // 录取
      handleEnroll(data) {
        console.log('录取')
        this.changeStatus(data)
      },
      changeStatus(data) {
        this.$api.recruitstat.masterAdmissionManageAdminssion({
          id: data.id,
          lqzt: data.lqzt
        }).then(res => {
          this.loadData({
            pageSize: 10,
            pageNum: this.pagination.current
          })
        })
      },
      // 取消录取
      handleCancelEnroll(data) {
        var that = this;
        this.$confirm({
          title: '确定取消录取?',
          onOk() {
            console.log('OK');
            that.changeStatus(data)
          },
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });
      },
      // 提交
      handleSubmit() {
        this.$confirm({
          title: '温馨提示',
          content: '请确定维护完本院系所有信息后再提交，一旦提交禁止再进行任何修改',
          okText: '保存',
          cancelText: '关闭',
          onOk: () => {
            console.log('保存')
            this.$api.recruitstat.masterAdmissionManageSub(this.queryParam).then(res => {
              this.loadData({
                pageSize: 10,
                pageNum: this.pagination.current
              })
            })
          },
          onCancel: () => {
            console.log('关闭')
          }
        })
      },
      // 关闭审查弹层
      handleReviewClose() {
        this.planReviewVisible = false
      },
      // 保存
      handleReviewSuccess() {
        this.planReviewVisible = false
        this.$message.success('修改成功')
        this.loadData({
          pageSize: 10,
          pageNum: this.pagination.current
        })
      },
      // 查看考生详情
      handlePreview(code) {
        this.$api.recruitstat.masterAdmissionManageId(code).then(res => {
          this.examineeData = res.data
          this.previewVisible = true
        })
      },
      // 关闭考生想去
      handleClosePreview() {
        this.previewVisible = false
      }
    }
  }
</script>

<style lang="less" scoped>
  .card {
    margin-bottom: 20px;
  }
</style>
